package com.module.sysconfig.controller;

import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.module.sysconfig.service.IZsZmxlhglService;
import com.module.sysconfig.service.impl.ZsZmxlhglServiceImpl;
import com.support.helper.AjaxHelper;
import com.support.jdbc.Page;
import com.support.observer.ReloadCacheEvent;
import com.support.util.StringUtils;
import com.support.web.ApplicationContextSupport;
import com.support.web.ControllerSupport;

/**
 * 编码规则
 * @author mfj
 *
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("/sysconfig")
public class ZsZmxlhglController extends ControllerSupport 
{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IZsZmxlhglService zsZmxlhglService;
	
//	protected ApplicationContext applicationContext = this.getContext();
	/**
	 * 新增 证书序列号
	 * @throws IOException
	 */
	@RequestMapping("/zs/saveZsxlh")
	public void saveZsxlh() throws IOException
	{
		try
		{
			Map<String, String> params = this.getParamsMap();
			
			zsZmxlhglService.saveXtZsxlh(params);
			
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "保存 证书序列号成功！"));
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BmgzController.saveZsxlh:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	}
	
	/**
	 * 删除 编码规则
	 * @throws IOException
	 */
	@RequestMapping("/zs/deleteZsxlh")
	public void deleteZsxlh() throws IOException
	{
		try
		{
			Map<String, String> params = this.getParamsMap();
			
			String id = params.get("ID");
			
			if(StringUtils.isNullOrEmpty(params.get("ID")))
			{
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "参数错误，ID未传"));
			}
			else
			{
				zsZmxlhglService.deleteZsxlh(id);
				
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "删除 证书序列号成功！"));
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BmgzController.deleteZsxlh:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	}
	
	/**
	 * 修改 编码规则
	 * @throws IOException
	 */
	@RequestMapping("/zs/updateZsxlh")
	public void updateZsxlh() throws IOException
	{
		try
		{
			Map<String, String> params = this.getParamsMap();
			
			if(StringUtils.isNullOrEmpty(params.get("ID")))
			{
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "参数错误，ID未传"));
			}
			else
			{
				zsZmxlhglService.updateXtZsxlh(params);
				
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "修改 证书序列号成功！"));
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BmgzController.updateZsxlh:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	}
	
	/**
	 * 查询 编码规则
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/zs/queryZsxlh")
	public void queryZsxlh() throws IOException
	{
		try
		{
			Page page = this.getParamsPage();
			
			page = zsZmxlhglService.queryXtZsxlh(page);
			
			returnJSONView(AjaxHelper.toJSON(page, AjaxHelper.SUCCESS, "查询 证书序列号成功！"));
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BmgzController.queryZsxlh:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	}
	
	
	
	
	/**
	 * 新增 编码规则详情
	 * @throws IOException
	 */
	@RequestMapping("/zm/saveZmxlh")
	public void saveZmxlh() throws IOException
	{
		try
		{
			Map<String, String> params = this.getParamsMap();
			
			zsZmxlhglService.saveXtZmxlh(params);
			
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "保存 证明序列号成功！"));
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BmgzController.saveZmxlh:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	}
	
	/**
	 * 删除 编码规则详情
	 * @throws IOException
	 */
	@RequestMapping("/zm/deleteZmxlh")
	public void deleteZmxlh() throws IOException
	{
		try
		{
			Map<String, String> params = this.getParamsMap();
			
			String id = params.get("ID");
			
			if(StringUtils.isNullOrEmpty(params.get("ID")))
			{
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "参数错误，ID未传"));
			}
			else
			{
				zsZmxlhglService.deleteXtZmxlh(id);
				
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "删除 证明序列号成功！"));
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BmgzController.deleteZmxlh:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	}
	
	/**
	 * 修改 编码规则详情
	 * @throws IOException
	 */
	@RequestMapping("/zm/updateZmxlh")
	public void updateZmxlh() throws IOException
	{
		try
		{
			Map<String, String> params = this.getParamsMap();
			
			if(StringUtils.isNullOrEmpty(params.get("ID")))
			{
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "参数错误，ID未传"));
			}
			else
			{
				System.out.println("controller:"+zsZmxlhglService);
				zsZmxlhglService.updateXtZmxlh(params);
				
				// 发送事件重新加载缓存 TODO
//				ApplicationContextSupport.getContext().publishEvent(new ReloadCacheEvent(zsZmxlhglService, "queryAllXtZmxlh"));
				
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "修改 证明序列号成功！"));
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BmgzController.updateZmxlh:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	}
	
	/**
	 * 查询 编码规则详情
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/zm/queryZmxlh")
	public void queryZmxlh() throws IOException
	{
		try
		{
			Page page = this.getParamsPage();
			
			page = zsZmxlhglService.queryXtZmxlh(page);
			
			returnJSONView(AjaxHelper.toJSON(page, AjaxHelper.SUCCESS, "查询 证明序列号成功！"));
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error("BmgzController.queryZmxlh:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	}
	
}
